<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\ResetPasswordRequest;
use App\Models\Year;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Entrust\Role;
use Auth;

class UserController extends Controller
{
    public function roles()
    {
        return Role::whereNotIn('name',['admin'])->get();
    }

    public function index(Request $request)
    {
        $map = [];
        $request->name && $map['name'] = $request->name;

        if($request->department_id && $request->department_id != 'null')
        {
            $map['department_id'] = $request->department_id;
        }

        $user = User::with('roles')->where($map);

        $user->whereHas('roles', function ($q){
            $q->whereNotIn('name', ['admin']);
        });

        return $user->orderBy('id')->paginate(10);
    }

    public function update(Request $request, $id)
    {
        $user = User::find($id);

        $data = $request->all();
        $data['password'] = bcrypt($request->password);
        $user->roles()->sync(Role::where('id', $request->role_id)->pluck('id'));

        return response()->json($user->update($data));
    }

    public function show($id)
    {
        return User::find($id);
    }

    public function store(Request $request)
    {
        $data = $request->all();
        $data['password'] = bcrypt($request->password);

        $user = User::create($data);

        $user->roles()->sync(Role::where('id', $request->role_id)->pluck('id'));

        return $user;
    }

    public function delete(Request $request)
    {
        $ids = $request->input('ids');
        $res = User::whereIn('id',$ids)->forceDelete();
        return response()->json($res);
        //return response()->json(User::destroy($request->ids));
    }

    //初始化用户密码
    public function initialise($id)
    {
        $user = User::find($id);
        $user&&$user->update(['password'=>bcrypt('123456')]);
        return $user;
    }

    //修改密码
    public function reset_password(Request $request)
    {
        $oldpassword = $request->input('old_password');
        $data = $request->all();
        $rules = [
            'old_password'=>'required|between:6,20',
            'password'=>'required|between:6,20|confirmed',
            'password_confirmation'=>'required|between:6,20',
        ];
        $messages = [
            'required' => '密码不能为空',
            'between' => '密码必须是6~20位之间',
            'confirmed' => '新密码和确认密码不匹配'
        ];
        $validator = \Validator::make($data, $rules, $messages);
        $user = Auth::user();
        $validator->after(function($validator) use ($oldpassword, $user) {
            if (!\Hash::check($oldpassword, $user->password)) {
                $validator->errors()->add('old_password', '原密码错误');
            }
        });
        if ($validator->fails()) {
            return [
                'success' => false,
                'message' => '原密码有误,请重新输入'
            ];  //返回一次性错误
        }
        $user->password = bcrypt($request->password);
        $user->save();
        return [
            'success' => true,
            'message' => '修改成功'
        ];
    }


    public function createYear(){
        $res = Year::firstOrCreate([
            "value" => date('Y'),
        ]);
        return response()->json($res);
    }
}
